package com.baidu.lib_common.view

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.BlurMaskFilter
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.net.Uri
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import com.baidu.lib_common.R
import com.baidu.lib_common.utils.dp2px

class HeadView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    View(context, attrs, defStyleAttr) {
    private val mSize: Int = dp2px(100)
    private var shadowSize: Float = dp2px(15).toFloat()
    private val mShadowRadius: Int = dp2px(6)
    private val rectF = RectF()
    private val mPaint = Paint()
    private val mShadowPaint = Paint()
    private var mOuterColor = Color.parseColor("#F8F4E3")
    var mHeadOuterPaint: Paint = Paint()
    private var bitmap: Bitmap? = null
    private var imageUri: Uri? = null
    private val mProgressColor: Int
    private val mText: String?
    private val mTextColor: Float
    private val mTextSize: Float
    var roundCanvasPath: Path = Path()
    var roundOuterCanvasPath: Path = Path()

    init {
        init()
        val defaultSize = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,
            14f,
            context.resources.displayMetrics
        )

        //从attrs中解析自己定义的属性的值
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.HeadView)
        //解析文本 尺寸 颜色
        mOuterColor = typedArray.getColor(R.styleable.HeadView_backgroundCircleColor, Color.WHITE)
        mProgressColor = typedArray.getColor(R.styleable.HeadView_headProgressColor, Color.WHITE)
        mText = typedArray.getString(R.styleable.HeadView_text)
        mTextColor = typedArray.getColor(R.styleable.HeadView_textColor, Color.WHITE).toFloat()
        shadowSize = typedArray.getDimension(R.styleable.HeadView_shadowSize, defaultSize)
        mTextSize = typedArray.getDimension(R.styleable.HeadView_textSize, defaultSize)
        //释放内存
        typedArray.recycle()
        mHeadOuterPaint.color = mOuterColor
    }

    private fun init() {
        setLayerType(LAYER_TYPE_SOFTWARE, null)
        bitmap = BitmapFactory.decodeResource(resources, R.drawable.play_back)
        val blurFilter = BlurMaskFilter(mShadowRadius.toFloat(), BlurMaskFilter.Blur.NORMAL)
        mShadowPaint.setMaskFilter(blurFilter)
        mShadowPaint.setShadowLayer(mShadowRadius.toFloat(), 0f, 0f, Color.parseColor("#80000000"))
        mShadowPaint.color = Color.parseColor("#FFFFFF")
        mHeadOuterPaint.color = mOuterColor
        mHeadOuterPaint.style = Paint.Style.STROKE
        mHeadOuterPaint.strokeWidth = dp2px(3).toFloat()
    }


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)

        roundCanvasPath.addRoundRect(
            shadowSize,
            shadowSize,
            width - shadowSize,
            height - shadowSize,

            9999f,
            9999f,
            Path.Direction.CW

        )
        roundOuterCanvasPath.addRoundRect(
            0f,
            0f,
            width.toFloat(),
            height.toFloat(),
            9999f,
            9999f,
            Path.Direction.CW
        )
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val desireWidth = mSize
        val desireHeight = mSize
        val realWidth = resolveSizeAndState(desireWidth, widthMeasureSpec, MEASURED_STATE_TOO_SMALL)
        val realHeight = realWidth
        setMeasuredDimension(realWidth, realWidth)
    }

    override fun onDraw(canvas: Canvas) {
        canvas.save()
        canvas.clipPath(roundOuterCanvasPath)
        drawShadow(canvas)
        canvas.restore()
        canvas.save()
        canvas.clipPath(roundCanvasPath)
        drawHead(canvas)
        drawHeadOuter(canvas)
        canvas.restore()
    }

    private fun drawHeadOuter(canvas: Canvas) {
        rectF.left = shadowSize
        rectF.top = shadowSize
        rectF.right = width - shadowSize
        rectF.bottom = height - shadowSize


        canvas.drawCircle(
            (width / 2).toFloat(),
            (height / 2).toFloat(),
            (width - 2 * shadowSize) / 2,
            mHeadOuterPaint
        )
    }

    private fun drawShadow(canvas: Canvas) {
        rectF.left = shadowSize
        rectF.top = shadowSize
        rectF.right = width - shadowSize
        rectF.bottom = height - shadowSize
        canvas.drawCircle(
            (width / 2).toFloat(),
            (height / 2).toFloat(),
            (width - 2 * shadowSize) / 2,
            mShadowPaint
        )
    }

    fun setImageBitmap(bitmap: Bitmap?) {
        this.bitmap = bitmap
        invalidate()
    }

    fun setImageUri(imageUri: Uri?) {
        this.imageUri = imageUri
    }

    private fun drawHead(canvas: Canvas) {
        rectF.left = shadowSize
        rectF.top = shadowSize
        rectF.right = width - shadowSize
        rectF.bottom = height - shadowSize

        canvas.drawBitmap(bitmap!!, null, rectF, mPaint)
    }
}